index_app.controller("comm_consum_list", function ($scope,$http,$timeout) {

	var grid_selector = "#grid-table";
	var pager_selector = "#grid-pager";


	$(window).on('resize.jqGrid', function() {
		$(grid_selector).jqGrid('setGridWidth', $(".page-content").width());
	});

	 $http.post("commConsum/getSumTotal").then(function(response){
            	//数据源
            	$scope.sumTotal = response.data;
         });


	$http.post("commodityType/selectAll").then(function(response){
    		//数据源
    	$scope.commType = response.data;
    	$scope.commTypeSelect = "<select id='commTypeSelect'>";
    	$scope.typeOptions = "<option>所属类别</option>";
    	for(var i = 0;i<$scope.commType.length;i++){
    	    $scope.typeOptions = $scope.typeOptions+ "<option>"+$scope.commType[i].skuType+"</option>"

    	}
    	$scope.commTypeSelect = $scope.commTypeSelect + $scope.typeOptions +"</select>";

    	$scope.paymentSelect = "<select id='paymentSelect'><option>付款方式</option><option>支付宝</option><option>微信</option><option>现金</option><option>银联</option></select>";

    	 jQuery(grid_selector).jqGrid('aceInit', pager_selector, {
         		url:"commConsum/listCommConsum",
         		datatype : "json",
         		colNames : ['','ID', '日期','会员号','商品名称','所属类别 '+$scope.commTypeSelect,'单价','数量','总额', '付款方式 '+$scope.paymentSelect,'订单号','操作员'],
         		colModel : [
         		            {name:'myac',hidden:true},
         		            {name:'conKey',index:'conKey', width:60,hidden:true},
         		            {name:'date',index:'date', width:60},
         		            {name:'vipNum',index:'vipNum', width:60},
         		            {name:'skuName',index:'skuName', width:60},
         		            {name:'skuType',index:'skuType', width:100},
         		            {name:'price',index:'price', width:60},
         		             {name:'quantity',index:'quantity', width:60},
         		             {name:'sum',index:'sum', width:60},
         		             {name:'paymentMethod',index:'paymentMethod', width:100},
         		             {name:'comConOrder',index:'comConOrder', width:60},
         		            {name:'operatorNum.usrName',index:'operatorNum', width:60 },
         		            ],
         		            sortable:true,
         		            sortname:'conKey',
         		            sortorder:'asc',
         		            caption : "<i class='menu-icon fa fa-windows white'></i> 商品明细"
         	});
         	jQuery(grid_selector).jqGrid('aceNavGrid', pager_selector);

            $('#paymentSelect').change(function(){
                $("#export").hide();
             	    $scope.selectedVal=$(this).children('option:selected').val();
             	    $scope.type="paymentMethod";
             	    if($scope.selectedVal == "付款方式"){
             	        $scope.type = "";
             	    }
             	     console.log($scope.type);
                     $("#grid-table").jqGrid('setGridParam',{
                          url:"commConsum/searchSelf?field="+$scope.type+"&fieldValue="+$scope.selectedVal,
                          datatype:'json',
                           page:1
                      }).trigger("reloadGrid");
                 });

                  $('#commTypeSelect').change(function(){
                    $("#export").hide();
                              	    $scope.selectedVal=$(this).children('option:selected').val();
                                     console.log($scope.selectedVal);
                              	    $scope.type="skuType";
                              	    if($scope.selectedVal == "所属类别"){
                              	        $scope.type = "";
                              	    }
                              	     console.log($scope.type);
                                      $("#grid-table").jqGrid('setGridParam',{
                                           url:"commConsum/searchSelf?field="+$scope.type+"&fieldValue="+$scope.selectedVal,
                                           datatype:'json',
                                            page:1
                                       }).trigger("reloadGrid");
                                  });
    });

 $scope.refresh = function(){
    $("#comConsumInof").val("");
    $("#startDate").val("");
    $("#endDate").val("");
        $(grid_selector).jqGrid('setGridParam',{
                          url:"commConsum/listCommConsum",
                          datatype:'json',
                           page:1
                    }).trigger("reloadGrid");
    }

	//});

	//查找事件
    $scope.searchByComCon = function(){
        $(grid_selector).jqGrid('setGridParam',{
              url:"commConsum/searchSelf?field=comConOrder&fieldValue="+$("#comConsumInof").val(),
              datatype:'json',
               page:1
        }).trigger("reloadGrid");
    }
      $("#export").hide();
    $scope.searchTime = function(){
        if(!checkEndTime($scope.startDate,$scope.endDate)){
            $scope.title = "警告";
            $scope.toolTip = "开始时间小于结束时间，请重新选择";
            toolTipDialog();
        } else {
            console.log($scope.startDate);
             $("#export").show();
            $(grid_selector).jqGrid('setGridParam',{
                  url:"commConsum/searchDate?startDate="+$scope.startDate+"&endDate="+$scope.endDate,
                  datatype:'json',
                  page:1
            }).trigger("reloadGrid");
            toExcel();
        }
    }

     function toExcel(){
        $scope.exportExcel = function(){
            $scope.actionStr = "/commConsum/toDcExcel?startDate="+$scope.startDate+"&endDate="+$scope.endDate;
            $("#formExcel").attr("action", $scope.actionStr);
        }
     }

    function checkEndTime(startTime,endTime){
        $scope.start=new Date(startTime.replace("-", "/").replace("-", "/"));
        $scope.end=new Date(endTime.replace("-", "/").replace("-", "/"));

        if($scope.end < $scope.start){
            return false;
        }
        return true;
    }
function toolTipDialog(){
    		easyDialog.open({
    			container : {
    				header : $scope.title,
    				content : $scope.toolTip,
    				yesFn : $scope.toolFn,
    				noFn : false
    			},
    			fixed : false
    		});
    	}
    	//弹窗中确定按钮事件
    	$scope.toolFn = function(){
    		return true;
    	}

})


